package com.backstage.dao.login;

import com.backstage.entity.Permission;
import com.backstage.entity.Role;
import com.backstage.entity.UserEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by cynic on 2018/4/10.
 */
@Repository
public interface LoginMapper {

    /**
     * 根据用户账号查询用户信息
     *
     * @param userName
     * @return
     */
    @Select("select * from user_info where username = #{userName}")
//    @Results({
//            @Result(property = "roles", javaType = List.class, column = "uId",
//                    many = @Many(select = "com.backstage.dao.login.LoginMapper.getRolesByUserId"))
//    })
    UserEntity getUserByUserName(String userName);

    /**
     * 根据用户id获取用户角色
     *
     * @param uId
     * @return
     */
    @Select("select * from user_role where uid = #{uId}")
    @Results({
            @Result(property = "roles", javaType = List.class, column = "uId",
                    many = @Many(select = "com.backstage.dao.login.LoginMapper.getPermissionsByRoleId"))
    })
    List<Role> getRolesByUserId(String uId);

    /**
     * 根据角色id获取角色权限
     *
     * @param roleId
     * @return
     */
    @Select("select * from role_permission where rid = #{roleId}")
    List<Permission> getPermissionsByRoleId(String roleId);
}
